package com.ljy.farm.service.person;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ljy.farm.constants.BusinessConstants;
import com.ljy.farm.constants.ExceptionConstants;
import com.ljy.farm.datasource.mappers.AccountHeadMapperEx;
import com.ljy.farm.datasource.mappers.DepotHeadMapperEx;
import com.ljy.farm.datasource.mappers.PersonMapper;
import com.ljy.farm.datasource.mappers.PersonMapperEx;
import com.ljy.farm.exception.BusinessRunTimeException;
import com.ljy.farm.exception.JshException;
import com.ljy.farm.service.log.LogService;
import com.ljy.farm.service.user.UserService;
import com.ljy.farm.utils.StringUtil;
import com.ljy.farm.datasource.entities.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class PersonService {
    private Logger logger = LoggerFactory.getLogger(PersonService.class);

    @Resource
    private PersonMapper personMapper;
    @Resource
    private PersonMapperEx personMapperEx;
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;
    @Resource
    private AccountHeadMapperEx accountHeadMapperEx;
    @Resource
    private DepotHeadMapperEx depotHeadMapperEx;

    public Person getPerson(long id) throws Exception {
        Person result = null;
        try {
            result = personMapper.selectByPrimaryKey(id);//根据personmapper的select方法创建一个person对象
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return result;
    }

    public List<Person> getPerson() throws Exception {//根据example得到的一个person集合
        PersonExample example = new PersonExample();
        example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Person> list = null;
        try {
            list = personMapper.selectByExample(example);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return list;
    }

    public List<Person> select(String name, String type, int offset, int rows) throws Exception {
        List<Person> list = null;
        try {
            list = personMapperEx.selectByConditionPerson(name, type, offset, rows);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return list;
    }

    public Long countPerson(String name, String type) throws Exception {
        Long result = null;
        try {
            result = personMapperEx.countsByPerson(name, type);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertPerson(String beanJson, HttpServletRequest request) throws Exception {
        Person person = JSONObject.parseObject(beanJson, Person.class);
        int result = 0;
        try {
            result = personMapper.insertSelective(person);
            logService.insertLog("经手人", BusinessConstants.LOG_OPERATION_TYPE_ADD, request);
        } catch (Exception e) {
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updatePerson(String beanJson, Long id, HttpServletRequest request) throws Exception {
        Person person = JSONObject.parseObject(beanJson, Person.class);
        person.setId(id);
        int result = 0;
        try {
            result = personMapper.updateByPrimaryKeySelective(person);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(id).toString(), request);
        } catch (Exception e) {
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deletePerson(Long id, HttpServletRequest request) throws Exception {
        int result = 0;
        try {
            result = personMapper.deleteByPrimaryKey(id);
            logService.insertLog("经手人",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(id).toString(), request);
        } catch (Exception e) {
            JshException.writeFail(logger, e);
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeletePerson(String ids, HttpServletRequest request) throws Exception {
        List<Long> idList = StringUtil.strToLongList(ids);
        PersonExample example = new PersonExample();
        example.createCriteria().andIdIn(idList);
        int result = 0;
        try {
            result = personMapper.deleteByExample(example);
            logService.insertLog("经手人", "批量删除,id集:" + ids, request);
        } catch (Exception e) {
            JshException.writeFail(logger, e);
        }
        return result;
    }

    public int checkIsNameExist(Long id, String name) throws Exception {
        PersonExample example = new PersonExample();
        example.createCriteria().andIdNotEqualTo(id).andNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<Person> list = null;
        try {
            list = personMapper.selectByExample(example);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return list == null ? 0 : list.size();
    }

    public String getPersonByIds(String personIDs) throws Exception {
        List<Long> ids = StringUtil.strToLongList(personIDs);
        PersonExample example = new PersonExample();
        example.createCriteria().andIdIn(ids).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        example.setOrderByClause("Id asc");
        List<Person> list = null;
        try {
            list = personMapper.selectByExample(example);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        StringBuffer sb = new StringBuffer();
        if (null != list) {
            for (Person person : list) {
                sb.append(person.getName() + " ");
            }
        }
        return sb.toString();
    }

    public List<Person> getPersonByType(String type) throws Exception {
        PersonExample example = new PersonExample();
        example.createCriteria().andTypeEqualTo(type).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        example.setOrderByClause("Id asc");
        List<Person> list = null;
        try {
            list = personMapper.selectByExample(example);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        return list;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeletePersonByIds(String ids) throws Exception {
        logService.insertLog("经手人",
                new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_DELETE).append(ids).toString(),
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
        User userInfo = userService.getCurrentUser();
        String[] idArray = ids.split(",");
        int result = 0;
        try {
            result = personMapperEx.batchDeletePersonByIds(new Date(), userInfo == null ? null : userInfo.getId(), idArray);
        } catch (Exception e) {
            JshException.writeFail(logger, e);
        }
        return result;
    }

    /**
     * create by: qiankunpingtai
     * website：https://qiankunpingtai.cn
     * description:
     * 正常删除，要考虑数据完整性，进行完整性校验
     * create time: 2019/4/10 15:14
     *
     * @return int
     * @Param: ids
     */
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeletePersonByIdsNormal(String ids) throws Exception {
        /**
         * 校验
         * 1、财务主表	jsh_accounthead
         * 2、单据主表	jsh_depothead
         * 是否有相关数据
         * */
        int deleteTotal = 0;
        if (StringUtils.isEmpty(ids)) {
            return deleteTotal;
        }
        String[] idArray = ids.split(",");
        /**
         * 校验财务主表	jsh_accounthead
         * */
        List<AccountHead> accountHeadList = null;
        try {
            accountHeadList = accountHeadMapperEx.getAccountHeadListByHandsPersonIds(idArray);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        if (accountHeadList != null && accountHeadList.size() > 0) {
            logger.error("异常码[{}],异常提示[{}],参数,HandsPersonIds[{}]",
                    ExceptionConstants.DELETE_FORCE_CONFIRM_CODE, ExceptionConstants.DELETE_FORCE_CONFIRM_MSG, ids);
            throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
                    ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
        }
        /**
         * 校验单据主表	jsh_depothead
         * */
        List<DepotHead> depotHeadList = null;
        try {
            depotHeadList = depotHeadMapperEx.getDepotHeadListByHandsPersonIds(idArray);
        } catch (Exception e) {
            JshException.readFail(logger, e);
        }
        if (depotHeadList != null && depotHeadList.size() > 0) {
            logger.error("异常码[{}],异常提示[{}],参数,HandsPersonIds[{}]",
                    ExceptionConstants.DELETE_FORCE_CONFIRM_CODE, ExceptionConstants.DELETE_FORCE_CONFIRM_MSG, ids);
            throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
                    ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
        }
        /**
         * 校验通过执行删除操作
         * */
        deleteTotal = batchDeletePersonByIds(ids);
        return deleteTotal;
    }
}
